package com.unytech.project.web.action;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.unytech.project.entity.security.ErrorRecord;
import com.unytech.project.utils.SystemParams;

public abstract class BaseAction extends ActionSupport implements ActionResult, ServletRequestAware, 
		ServletResponseAware, ServletContextAware {
	
	final protected Log logger = LogFactory.getLog(getClass());
	//Action的统一返回信息
	/** 返回到初始化系统的视图 */
//	public final static String INITSYSTEM = "initsystem";
	
	protected ErrorRecord errorrecord = new ErrorRecord();
	
	@Resource protected SystemParams systemParams; //注入一个系统参数配置类
	//BaseService service = null;  //每一个Action都需要一个service，可注入需要的现类
//	protected OperateRecord operaterecord = new OperateRecord();
	
	//通过IoC的方式来获得Action封装后的 request,response,session  
	private HttpServletRequest request;  
    private HttpServletResponse response;  
	private ServletContext servletcontext;
    private HttpSession session;
	
    
//    public BufferedImage verifyCodeFactory(Integer codelen) throws Exception{
//    	Producer kaptcha = null;
//		Properties proper = null;
//		BufferedImage bufimage = null;
//		try {
//			//这个方法要换
////			String filepath = getRequest().getRealPath("/WEB-INF/classs/kaptcha.properties");
////			proper = PropertiesUtils.readProperties(filepath);  //自己写的方法
//			proper = PropertiesUtils.loadProperties("kaptcha.properties");  //Springside的方法
//		} catch (Exception e) {
//			addActionError("未找到kaptcha.properties文件，将按照默认属性生成验证码！");
//			e.printStackTrace();
//		} finally {
//			String capText = null;
//			try {
//				if (proper == null || proper.size() == 0) {
//					proper = new Properties();
//					proper.put("kaptcha.session.key", Globals.SESSION_VERIFYCODE_KEY);
//					proper.put("kaptcha.textproducer.char.length", codelen);
//				}
//				Config config = new Config(proper);
//				kaptcha = config.getProducerImpl();
//				capText = kaptcha.createText();
//
//				logger.info("BaseAction生成的验证码："+capText);
//				getSession().setAttribute(Globals.SESSION_VERIFYCODE_KEY, capText);  
//				bufimage = kaptcha.createImage(capText); 
//			} catch (Exception e) {
//				addActionError("生成验证码失败！");
//				e.printStackTrace();
//				return null;
//			}
//		}
//    	return bufimage;
//    }
    
    /**
     * 检查调用用于生成验证码的参数的合法性
     * @param codelength 验证码长度
     * @param width 验证码宽度
     * @param height 验证码高度
     * @return
     */
    public Integer[] CheckVerifyCode(String length, String width, String height) {
    	Integer[] codeparam = new Integer[3];
    	if (length == null) {
    		codeparam[0] = null;
    	}
    	else {
    		if (Integer.parseInt(length) < Integer.parseInt(systemParams.getMinverifycodelength()) 
    				|| Integer.parseInt(length) > Integer.parseInt(systemParams.getMaxverifycodelength())) {
    			codeparam[0] = null;
    		}
    		else {
    			codeparam[0] = Integer.parseInt(length);
    		}
    	}
    	
    	if (width == null) {
    		codeparam[1] = null;
    	}
    	else {
    		if (Integer.parseInt(width) < Integer.parseInt(systemParams.getMinverifycodewidth()) 
    				|| Integer.parseInt(width) > Integer.parseInt(systemParams.getMaxverifycodewidth())) {
    			codeparam[1] = null;
    		}
    		else {
    			codeparam[1] = Integer.parseInt(width);
    		}
    	}
    	
    	if (height == null) {
    		codeparam[2] = null;
    	}
    	else {
    		if (Integer.parseInt(height) < Integer.parseInt(systemParams.getMinverifycodeheight()) 
    				|| Integer.parseInt(height) > Integer.parseInt(systemParams.getMaxverifycodeheight())) {
    			codeparam[2] = null;
    		}
    		else {
    			codeparam[2] = Integer.parseInt(height);
    		}
    	}
    		
    	return codeparam;
    }
    
    
    
    
    
	//必须有的方法，为注入自动注入所用
    @Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request; 
		this.session = request.getSession();
	}	
    @Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	@Override
	public void setServletContext(ServletContext servletcontext) {
		this.servletcontext = servletcontext;
	}
	
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public ServletContext getServletContext() {
		return servletcontext;
	}
	public HttpSession getSession() {
		return session;
	}
	
}
